package org.example.struct.array;

import com.alibaba.fastjson2.JSON;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author liyishan
 * @date 2023/8/17 09:35
 * @apiNote
 */

public class Test01002CommonChars {

    public static void main(String[] args) {
        String[] words = {"bella","label","roller"};
        List<String> list = commonChars(words);
        System.out.println(JSON.toJSON(list));
    }
    public static List<String> commonChars(String[] words) {
        int[] temp = new int[26];
        Arrays.fill(temp,Integer.MAX_VALUE);
        for (String word : words) {
            int[] freq = new int[26];
            int length = word.length();
            for(int i =0;i<length;i++){
                char ch = word.charAt(i);
                freq[ch-'a']++;
            }
            for(int i =0;i<26;i++){
                temp[i] = Math.min(temp[i],freq[i]);
            }
        }
        List<String> ans = new ArrayList<>();
        for(int i = 0;i<26;i++){
            for(int j = 0;j<temp[i];j++){
                ans.add(String.valueOf((char)(i+'a')));
            }
        }
        return ans;
    }

}
